#include<bits/stdc++.h>
using namespace std;
//clear
void clear(){
    system("clear");
    cout<<"clear completely!\n";
}
//data define
typedef struct {
    string name;
    int age;
    string number;
}line;

//node define
typedef struct node{
    line data;
    struct node* next;
    struct node* priority;
}node,*dlist;

//init
bool init(dlist &L){
    L=(node*)malloc(sizeof(node));
    if(L==NULL){
        cout<<"create error!\n";
        return false;
    }
    else{
        L->next=NULL;
        L->priority=NULL;
        cout<<"create completely!\n";
        return true;
    }

}

//destroy 
bool destory(dlist &L){
    free(L);
    if(L==NULL){
        cout<<"destory completely!\n";
        return true;
    }
    else{
        cout<<"destory fail!\n";
        return false;
    }
}

//forward create
dlist forward_create(dlist &L){
    init(L);
    node *s,*r=L;
    line x;
    cout<<"input age of the student, -1 to stop:\n";
    cin>>x.age;
    if(x.age==-1){
        cout<<"error! this is the first line!\n";
        return L;
    } 
    cout<<"input the name:\n";
    cin>>x.name;
    cout<<"input the number:\n";
    cin>>x.number;
    while(x.age!=-1){
        s=(node*)malloc(sizeof(node));
        s->data.age=x.age;
        s->data.name=x.name;
        s->data.number=x.number;
        s->priority=r;
        r->next=s;
        r=s;
        cout<<"input age of the student, -1 to stop:\n";
        cin>>x.age;
        if(x.age==-1) break;
        cout<<"input the name:\n";
        cin>>x.name;
        cout<<"input the number:\n";
        cin>>x.number;

    }
    r->next=NULL;
    s->next=NULL;
    return L;
}

//print list node
void print_list(dlist L){
    node* p=L->next;
    int cnt=1;
    while(p!=NULL){
        cout<<"--------------line "<<cnt<<"---------------\n";
        cout<<"name: "<<p->data.name<<"\n";
        cout<<"age: "<<p->data.age<<"\n";
        cout<<"number: "<<p->data.number<<"\n";
        cout<<"-----------------------------------\n";
        p=p->next;
        cnt++;
    }
}
//find_with_index
/**
 * @brief when you 传入() an index,this function
 * will find the index-1 node,and return p,you can 
 * use p->next to get the element you find,now
 * p->next will show the index you find.That's
 * why we find index-1.
 */
dlist find_with_index(dlist &L,int index){
    node *p=L;
    int cnt=0;
    while(cnt<index&&p!=NULL){
        p=p->next;
        cnt++;
    }
    return p;
}
//insert with index
dlist insert_with_index(dlist &L,int index){
    line x;
    cout<<"index "<<index<<" will be insert!\n";
    cout<<"input the age:\n";
    cin>>x.age;
    cout<<"input the name:\n";
    cin>>x.name;
    cout<<"input the number:\n";
    cin>>x.number;
    node* tmp=(node*)malloc(sizeof(node));
    tmp->data.age=x.age;
    tmp->data.name=x.name;
    tmp->data.number=x.number;
    /*find index-1,if you want to insert index...*/
    node* find_node=find_with_index(L,index-1);
    tmp->next=find_node->next;
    tmp->priority=find_node;
    find_node->next=tmp;
    cout<<"insert successed!\n";
    return L;
}
int main(){

    dlist L;
    forward_create(L);
    print_list(L);
    // node* p=find_with_index(L,3);
    // cout<<p->data.age;
    insert_with_index(L,3);
    print_list(L);

return 0;
}